package com.ljh.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljh.mapper.listMapper;
import com.ljh.pojo.Singer;
import com.ljh.pojo.User;
import com.ljh.pojo.UserBefore;
import com.ljh.pojo.admin;
import com.ljh.service.ListService;
import com.ljh.service.UserService;
import com.ljh.utils.JwtUtils;
import com.ljh.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@Api(description = "用户接口管理")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    ListService listService;

    /**
     * 微信登录函数
     * @param userBefore
     * @return
     */
    @RequestMapping("login")
    @ApiOperation(value = "登录")
    public Result login(@RequestBody UserBefore userBefore){
        System.out.println(userBefore);
        String openId = userService.getOpenId(userBefore.getCode());
        userBefore.setUserPassword(openId);
        User user = userService.userLogin(userBefore);
        if(user!=null){
            return Result.success(user);
        }else{
            return Result.fail(400, "登入失败");
        }
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @RequestMapping("updateUser")
    @ApiOperation(value = "更新用户")
    public Result updateUser(@RequestBody User user){
        System.out.println(user);
        Integer integer = userService.UpdatePersonInfo(user);
        User byId = userService.findById(user.getUserId());
        if(integer!=0){
            return Result.success(byId);
        }else{
            return Result.fail(400, "修改失败");
        }
    }

    @GetMapping("findSingerBySongId")
    @ApiOperation(value = "通过歌曲Id来查找用户")
    public Result findSingerBySongId(Integer songId){
        Singer bySongId = userService.findBySongId(songId);
        return Result.success(bySongId);
    }

    /**
     * 分页
     * @param current
     * @param size
     * @param userName
     * @return
     */
    @GetMapping("findUser")
    @ApiOperation(value = "用户列表")
    @CrossOrigin
    public Result userList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String userName
    ) {
        IPage<Singer> page = userService.page(new Page(current,size),new QueryWrapper<User>()
                .like(StrUtil.isNotBlank(userName), "userName",userName));
        return Result.success(page);
    }

    @GetMapping("findUserById")
    @ApiOperation(value = "通过用户id查找用户")
    public Result findSongById(@RequestParam Integer userId){
        User userById = userService.findUserById(userId);
        return Result.success(userById);
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PostMapping("updateUser2")
    @ApiOperation(value = "更新用户")
    public Result updateUser2(@RequestBody User user){
        System.out.println(user);
        Integer integer = userService.UpdatePersonInfo2(user);
        if(integer!=0){
            return Result.success("修改成功");
        }else{
            return Result.fail(400, "修改失败");
        }
    }

    /**
     * 修改个人图片
     * @param userPhoto
     * @param userId
     */
    @GetMapping("updatePhoto")
    @ApiOperation(value = "更新用户照片")
    public Result UpdatePersonPhoto(@RequestParam String userPhoto, @RequestParam Integer userId){
        userService.UpdatePersonPhoto(userPhoto, userId);
        return Result.success("修改成功");
    }

    @GetMapping("deleteUserById")
    @ApiOperation("通过用户id来删除用户")
    public Result deleteUserById(
            @RequestParam Integer userId
    ){
        listService.deleteLikeById(userId);
        listService.deleteListById(userId);
        userService.deleteUserById(userId);
        return Result.success("删除成功");
    }

    @GetMapping("/findUserSex")
    @ApiOperation(value = "查找用户的性别比例")
    public Result findUserSex(){
        return Result.success(userService.findUserSex());
    }

    @GetMapping("/login2")
    @ApiOperation(value = "利用密码登录")
    public Result loginMIma(String password,String username){
        System.out.println(password);
        System.out.println(username);
        String s = JwtUtils.create(password);
        List<admin> admins = userService.loginMI(s,username);
        if(admins!=null&&admins.size()>0){
            return Result.success(admins.get(0));
        }
        return Result.fail(40000, "登录失败");
    }
}
